import 'package:athena/components/gaps.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class AppTextField extends StatefulWidget {
  final TextEditingController? controller;
  final FocusNode? focusNode;
  final EdgeInsetsGeometry? padding;
  final Decoration? decoration;
  final Decoration? focusDecoration;
  final InputDecoration? inputDecoration;
  final TextInputType keyboardType;
  final TextInputAction? textInputAction;
  final TextStyle? style;
  final TextAlign textAlign;
  final String? hintText;
  final TextStyle? hintStyle;
  final Widget? prefix;
  final OverlayVisibilityMode prefixMode;
  final Widget? suffix;
  final OverlayVisibilityMode suffixMode;
  final Widget? clearWidget;
  final OverlayVisibilityMode clearButtonMode;
  final bool autofocus;
  final String obscuringCharacter;
  final bool obscureText;
  final int maxLines;
  final int? minLines;
  final bool readOnly;
  final bool? showCursor;
  final int? maxLength;
  final ValueChanged<String>? onChanged;
  final VoidCallback? onEditingComplete;
  final ValueChanged<String>? onSubmitted;
  final bool? enabled;
  final GestureTapCallback? onTap;
  final List<TextInputFormatter>? inputFormatters;

  AppTextField({
    Key? key,
    this.focusNode,
    this.inputDecoration,
    this.keyboardType = TextInputType.text,
    this.textInputAction,
    this.style,
    this.hintText,
    this.hintStyle,
    this.prefix,
    this.prefixMode = OverlayVisibilityMode.always,
    this.suffix,
    this.suffixMode = OverlayVisibilityMode.always,
    this.clearWidget,
    this.clearButtonMode = OverlayVisibilityMode.never,
    this.textAlign = TextAlign.start,
    this.autofocus = false,
    this.obscuringCharacter = '*',
    this.obscureText = false,
    this.maxLines = 1,
    this.minLines,
    this.readOnly = false,
    this.showCursor,
    this.maxLength,
    this.onChanged,
    this.onEditingComplete,
    this.onSubmitted,
    this.enabled,
    this.onTap,
    this.controller,
    this.inputFormatters,
    this.decoration,
    this.focusDecoration,
    this.padding,
  }) : super(key: key);

  @override
  _AppTextFieldState createState() {
    return _AppTextFieldState();
  }
}

class _AppTextFieldState extends State<AppTextField> {
  // late FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    // _focusNode = widget.focusNode ?? FocusNode();
    // _focusNode.addListener(() {
    //   setState(() {});
    // });
  }

  @override
  void dispose() {
    super.dispose();
  }

  bool _isShowFicWidget(OverlayVisibilityMode model) {
    switch (model) {
      case OverlayVisibilityMode.never:
        return false;
      case OverlayVisibilityMode.editing:
        return ObjectUtil.isNotEmpty(widget.controller?.text);
      case OverlayVisibilityMode.notEditing:
        return ObjectUtil.isEmpty(widget.controller?.text);
      case OverlayVisibilityMode.always:
        return true;
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        // _focusNode.requestFocus();
      },
      child: Container(
        padding: widget.padding,
        decoration: widget.decoration,
        child: Row(
          children: [
            Visibility(
              visible: _isShowFicWidget(widget.prefixMode),
              child: widget.prefix ?? Gaps.empty,
            ),
            Expanded(
              child: TextField(
                // focusNode: _focusNode,
                controller: widget.controller,
                style: widget.style,
                decoration: widget.inputDecoration ??
                    InputDecoration.collapsed(
                      hintText: widget.hintText,
                      hintStyle: widget.hintStyle,
                      border: InputBorder.none,
                    ),
                keyboardType: widget.keyboardType,
                textInputAction: widget.textInputAction,
                inputFormatters: widget.inputFormatters,
                onChanged: widget.onChanged,
                onSubmitted: widget.onSubmitted,
                onTap: widget.onTap,
                onEditingComplete: widget.onEditingComplete,
                enabled: widget.enabled,
                textAlign: widget.textAlign,
                autofocus: widget.autofocus,
                obscuringCharacter: widget.obscuringCharacter,
                obscureText: widget.obscureText,
                maxLines: widget.maxLines,
                minLines: widget.minLines,
                readOnly: widget.readOnly,
                showCursor: widget.showCursor,
                maxLength: widget.maxLength,
              ),
            ),
            Visibility(
              visible: _isShowFicWidget(widget.clearButtonMode),
              child: GestureDetector(
                  onTap: () {
                    widget.controller?.clear();
                    setState(() {});
                  },
                  child: widget.clearWidget ?? Icon(Icons.cancel, size: 16.0, color: Colors.grey)),
            ),
            Visibility(
              visible: _isShowFicWidget(widget.suffixMode),
              child: widget.suffix ?? Gaps.empty,
            ),
          ],
        ),
      ),
    );
  }
}

class AppTextFieldView extends StatefulWidget {
  final TextEditingController? controller;
  final FocusNode? focusNode;
  final EdgeInsetsGeometry? padding;
  final Decoration? decoration;
  final Decoration? focusDecoration;
  final InputDecoration? inputDecoration;
  final TextInputType keyboardType;
  final TextInputAction? textInputAction;
  final TextStyle? style;
  final TextAlign textAlign;
  final String? hintText;
  final TextStyle? hintStyle;
  final Widget? prefix;
  final OverlayVisibilityMode prefixMode;
  final Widget? suffix;
  final OverlayVisibilityMode suffixMode;
  final Widget? clearWidget;
  final OverlayVisibilityMode clearButtonMode;
  final bool autofocus;
  final String obscuringCharacter;
  final bool obscureText;
  final int maxLines;
  final int? minLines;
  final bool readOnly;
  final bool? showCursor;
  final int? maxLength;
  final ValueChanged<String>? onChanged;
  final VoidCallback? onEditingComplete;
  final ValueChanged<String>? onSubmitted;
  final bool? enabled;
  final GestureTapCallback? onTap;
  final List<TextInputFormatter>? inputFormatters;

  AppTextFieldView({
    Key? key,
    this.focusNode,
    this.inputDecoration,
    this.keyboardType = TextInputType.text,
    this.textInputAction,
    this.style,
    this.hintText,
    this.hintStyle,
    this.prefix,
    this.prefixMode = OverlayVisibilityMode.always,
    this.suffix,
    this.suffixMode = OverlayVisibilityMode.always,
    this.clearWidget,
    this.clearButtonMode = OverlayVisibilityMode.never,
    this.textAlign = TextAlign.start,
    this.autofocus = false,
    this.obscuringCharacter = '*',
    this.obscureText = false,
    this.maxLines = 1,
    this.minLines,
    this.readOnly = false,
    this.showCursor,
    this.maxLength,
    this.onChanged,
    this.onEditingComplete,
    this.onSubmitted,
    this.enabled,
    this.onTap,
    this.controller,
    this.inputFormatters,
    this.decoration,
    this.focusDecoration,
    this.padding,
  }) : super(key: key);

  @override
  _AppTextFieldViewState createState() {
    return _AppTextFieldViewState();
  }
}

class _AppTextFieldViewState extends State<AppTextFieldView> {
  late FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    _focusNode = widget.focusNode ?? FocusNode();
    _focusNode.addListener(() {
      setState(() {});
    });
  }

  @override
  void dispose() {
    super.dispose();
    _focusNode.dispose();
  }

  bool _isShowFicWidget(OverlayVisibilityMode model) {
    switch (model) {
      case OverlayVisibilityMode.never:
        return false;
      case OverlayVisibilityMode.editing:
        return ObjectUtil.isNotEmpty(widget.controller?.text);
      case OverlayVisibilityMode.notEditing:
        return ObjectUtil.isEmpty(widget.controller?.text);
      case OverlayVisibilityMode.always:
        return true;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: widget.padding,
      decoration: _focusNode.hasFocus ? widget.focusDecoration : widget.decoration,
      child: Row(
        children: [
          Visibility(
            visible: _isShowFicWidget(widget.prefixMode),
            child: widget.prefix ?? Gaps.empty,
          ),
          Expanded(
            child: TextField(
              focusNode: _focusNode,
              controller: widget.controller,
              style: widget.style,
              decoration: widget.inputDecoration ??
                  InputDecoration.collapsed(
                    hintText: widget.hintText,
                    hintStyle: widget.hintStyle,
                    border: InputBorder.none,
                  ),
              keyboardType: widget.keyboardType,
              textInputAction: widget.textInputAction,
              inputFormatters: widget.inputFormatters,
              onChanged: widget.onChanged,
              onSubmitted: widget.onSubmitted,
              onTap: widget.onTap,
              onEditingComplete: widget.onEditingComplete,
              enabled: widget.enabled,
              textAlign: widget.textAlign,
              autofocus: widget.autofocus,
              obscuringCharacter: widget.obscuringCharacter,
              obscureText: widget.obscureText,
              maxLines: widget.maxLines,
              minLines: widget.minLines,
              readOnly: widget.readOnly,
              showCursor: widget.showCursor,
              maxLength: widget.maxLength,
            ),
          ),
          Visibility(
            visible: _isShowFicWidget(widget.clearButtonMode),
            child: GestureDetector(
                onTap: () {
                  widget.controller?.clear();
                  setState(() {});
                },
                child: widget.clearWidget ?? Icon(Icons.cancel, size: 16.0, color: Colors.grey)),
          ),
          Visibility(
            visible: _isShowFicWidget(widget.suffixMode),
            child: widget.suffix ?? Gaps.empty,
          ),
        ],
      ),
    );
  }
}

class AppTextFieldOriginalView extends StatefulWidget {
  final TextEditingController? controller;
  final FocusNode? focusNode;
  final EdgeInsetsGeometry? padding;
  final Decoration? decoration;
  final Decoration? focusDecoration;
  final InputDecoration? inputDecoration;
  final TextInputType keyboardType;
  final TextInputAction? textInputAction;
  final TextStyle? style;
  final TextAlign textAlign;
  final String? hintText;
  final TextStyle? hintStyle;
  final Widget? prefix;
  final OverlayVisibilityMode prefixMode;
  final Widget? suffix;
  final OverlayVisibilityMode suffixMode;
  final Widget? clearWidget;
  final OverlayVisibilityMode clearButtonMode;
  final bool autofocus;
  final String obscuringCharacter;
  final bool obscureText;
  final int maxLines;
  final int? minLines;
  final bool readOnly;
  final bool? showCursor;
  final int? maxLength;
  final ValueChanged<String>? onChanged;
  final VoidCallback? onEditingComplete;
  final ValueChanged<String>? onSubmitted;
  final bool? enabled;
  final GestureTapCallback? onTap;
  final List<TextInputFormatter>? inputFormatters;

  AppTextFieldOriginalView({
    Key? key,
    this.focusNode,
    this.inputDecoration,
    this.keyboardType = TextInputType.text,
    this.textInputAction,
    this.style,
    this.hintText,
    this.hintStyle,
    this.prefix,
    this.prefixMode = OverlayVisibilityMode.always,
    this.suffix,
    this.suffixMode = OverlayVisibilityMode.always,
    this.clearWidget,
    this.clearButtonMode = OverlayVisibilityMode.never,
    this.textAlign = TextAlign.start,
    this.autofocus = false,
    this.obscuringCharacter = '*',
    this.obscureText = false,
    this.maxLines = 1,
    this.minLines,
    this.readOnly = false,
    this.showCursor,
    this.maxLength,
    this.onChanged,
    this.onEditingComplete,
    this.onSubmitted,
    this.enabled,
    this.onTap,
    this.controller,
    this.inputFormatters,
    this.decoration,
    this.focusDecoration,
    this.padding,
  }) : super(key: key);

  @override
  _AppTextFieldOriginalViewState createState() {
    return _AppTextFieldOriginalViewState();
  }
}

class _AppTextFieldOriginalViewState extends State<AppTextFieldOriginalView> {
  late FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    _focusNode = widget.focusNode ?? FocusNode();
  }


  @override
  void dispose() {
    super.dispose();
  }

  bool _isShowFicWidget(OverlayVisibilityMode model) {
    switch (model) {
      case OverlayVisibilityMode.never:
        return false;
      case OverlayVisibilityMode.editing:
        return ObjectUtil.isNotEmpty(widget.controller?.text);
      case OverlayVisibilityMode.notEditing:
        return ObjectUtil.isEmpty(widget.controller?.text);
      case OverlayVisibilityMode.always:
        return true;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: widget.padding,
      decoration: _focusNode.hasFocus ? widget.focusDecoration : widget.decoration,
      child: Row(
        children: [
          Visibility(
            visible: _isShowFicWidget(widget.prefixMode),
            child: widget.prefix ?? Gaps.empty,
          ),
          Expanded(
            child: TextField(
              focusNode: _focusNode,
              controller: widget.controller,
              style: widget.style,
              decoration: widget.inputDecoration ??
                  InputDecoration.collapsed(
                    hintText: widget.hintText,
                    hintStyle: widget.hintStyle,
                    border: InputBorder.none,
                  ),
              keyboardType: widget.keyboardType,
              textInputAction: widget.textInputAction,
              inputFormatters: widget.inputFormatters,
              onChanged: widget.onChanged,
              onSubmitted: widget.onSubmitted,
              onTap: widget.onTap,
              onEditingComplete: widget.onEditingComplete,
              enabled: widget.enabled,
              textAlign: widget.textAlign,
              autofocus: widget.autofocus,
              obscuringCharacter: widget.obscuringCharacter,
              obscureText: widget.obscureText,
              maxLines: widget.maxLines,
              minLines: widget.minLines,
              readOnly: widget.readOnly,
              showCursor: widget.showCursor,
              maxLength: widget.maxLength,
            ),
          ),
          Visibility(
            visible: _isShowFicWidget(widget.clearButtonMode),
            child: GestureDetector(
                onTap: () {
                  widget.controller?.clear();
                  setState(() {});
                },
                child: widget.clearWidget ?? Icon(Icons.cancel, size: 16.0, color: Colors.grey)),
          ),
          Visibility(
            visible: _isShowFicWidget(widget.suffixMode),
            child: widget.suffix ?? Gaps.empty,
          ),
        ],
      ),
    );
  }
}
